package me.khrystal.hotheaderdemo;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import me.khrystal.model.TestModel;
import me.khrystal.view.BaseViewHolder;
import me.khrystal.view.HorizontalListView;
import me.khrystal.view.TestHorizontalListViewAdapter;

public class MainActivity extends AppCompatActivity {
    UltimateRecyclerView recyclerView;
    List<TestModel> dataList;
    List<TestModel> headerList;
    SimpleAdapter adapter;
    TestHorizontalListViewAdapter testAdapter;
    TestModel follow;
    View headerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = (UltimateRecyclerView)findViewById(R.id.recycler_view);
        dataList = new ArrayList();
        adapter = new SimpleAdapter(this,dataList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        initHeaderView();
        for (int i=0;i<100;i++){
            TestModel tm = new TestModel();
            tm.id=i;
            tm.type = 0;
            tm.text = "第"+(i+1)+"行";
            dataList.add(tm);
        }
        //获取的网络数据
        follow = new TestModel();

        follow.type = 1;
        follow.text = "关注";
        Random random = new Random();
        if (follow!=null){
            dataList.set(random.nextInt(100),follow);
        }
//======================================================================


        adapter.notifyDataSetChanged();
        for (int i = 0; i < 100; i++) {
            TestModel tm = new TestModel();
            tm.id=i;
            tm.text = "第"+(i+1)+"个";
            headerList.add(tm);
        }
        testAdapter.notifyDataSetChanged();

    }

    private void initHeaderView() {
        headerView = getLayoutInflater().inflate(R.layout.hot_header,recyclerView.mRecyclerView,false);
        HorizontalListView listView = (HorizontalListView)headerView.findViewById(R.id.header_list_view);
        headerList = new ArrayList();
        testAdapter = new TestHorizontalListViewAdapter(this,headerList);
        listView.setAdapter(testAdapter);
        recyclerView.setNormalHeader(headerView);


    }


    public class SimpleAdapter extends UltimateViewAdapter<BaseViewHolder>{
        List<TestModel> dataList;
        Object mParent;
        //改列表通过网络加载

        public SimpleAdapter(Object parent ,List<TestModel> dataList){
            this.dataList = dataList;
            mParent = parent;
        }

        @Override
        public int getItemViewType(int position) {
            if (position < getItemCount() && (customHeaderView != null ? position <= dataList.size() : position < dataList.size()) && (customHeaderView != null ? position > 0 : true)) {
                TestModel testModel = dataList.get(customHeaderView != null ? position - 1 : position);
                return testModel.type;
            }
            return 1;
        }

        @Override
        public SimpleViewHolder getViewHolder(View view) {
            return new SimpleViewHolder(view,MainActivity.this,true);
        }

        @Override
        public SimpleViewHolder onCreateViewHolder(ViewGroup viewGroup) {
            return null;
        }

        @Override
        public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = null;
            BaseViewHolder holer = null;
           switch (viewType){
               case 0:
                   v = LayoutInflater.from(parent.getContext())
                           .inflate(R.layout.recycler_item,parent,false);
                   holer = new SimpleViewHolder(v,MainActivity.this,false);
                   break;
               case 1:
                   v = LayoutInflater.from(parent.getContext()).inflate(R.layout.follow_item,parent,false);
                   holer = new FollowViewHolder(v,MainActivity.this,false);
                   break;
           }
            return holer;
        }


        @Override
        public int getAdapterItemCount() {
            return dataList.size();
        }

        @Override
        public long generateHeaderId(int i) {
            return 0;
        }

        @Override
        public void onBindViewHolder(BaseViewHolder holder, int position) {
            if (position < getItemCount() && (customHeaderView != null ? position <= dataList.size() : position < dataList.size()) && (customHeaderView != null ? position > 0 : true)) {
                TestModel str = dataList.get(customHeaderView!=null? position-1:position);
                switch (getItemViewType(position)){
                    case 0:
                        SimpleViewHolder sv = (SimpleViewHolder)holder;
                        sv.bindData(str);
                        break;
                    case 1:
                        FollowViewHolder fv = (FollowViewHolder)holder;
                        fv.bindData(str);
                        break;
                }
            }
        }

        @Override
        public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup viewGroup) {
            return null;
        }

        @Override
        public void onBindHeaderViewHolder(RecyclerView.ViewHolder viewHolder, int i) {

        }

        public void append(TestModel item) {
            insert(dataList, item, dataList.size());
        }

        public void append(List<TestModel> items) {
            int pos = dataList.size();
            for (TestModel item : items) {
                    dataList.add(item);
            }
            notifyItemRangeInserted(pos, items.size());
        }

        public void remove(int position) {
            remove(dataList, position);
        }

        public void clear() {
            clear(dataList);
        }
    }


    public class SimpleViewHolder extends BaseViewHolder implements View.OnClickListener {
        TextView itemText;
        Activity activity;
        public SimpleViewHolder(View itemView,Activity activity,boolean skip) {
            super(itemView);
            if (skip)
                return;
            this.activity = activity;
            itemText = (TextView)(itemView.findViewById(R.id.recycler_text));
            itemText.setOnClickListener(this);

        }
        TestModel mTm;
        public void bindData(TestModel tm){
            mTm = tm;
            if (!TextUtils.isEmpty(mTm.text))
                itemText.setText(mTm.text);
        }

        @Override
        public void onClick(View v) {
            if (!TextUtils.isEmpty(mTm.text))
                Toast.makeText(activity,mTm.text,Toast.LENGTH_SHORT).show();
        }
    }

    public class FollowViewHolder extends BaseViewHolder implements View.OnClickListener{
        TextView itemText;
        Activity activity;

        public FollowViewHolder(View itemView,Activity activity,boolean skip){
            super(itemView);
            if (skip)
                return;
            this.activity = activity;
            itemText = (TextView)(itemView.findViewById(R.id.recycler_text));
            itemText.setOnClickListener(this);
        }

        TestModel mStr;
        public void bindData(TestModel str){
            mStr = str;
            if (!TextUtils.isEmpty(mStr.text))
                itemText.setText(mStr.text);
        }

        @Override
        public void onClick(View v) {
            if (!TextUtils.isEmpty(mStr.text))
                Toast.makeText(activity,mStr.text,Toast.LENGTH_SHORT).show();
        }
    }
}
